package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * <新闻 - 大江网> 解析类
 *
 * @author KK
 *
 */
public class JxnewsNewsAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		String[] regex={
				"http://.*.jxnews.com.cn/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml",
				};

		return RegexUtils.matchAnyIgnoreCase(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.main > div.main1 > div.contecter > div.con_left > div.pj_title > h1").text();
			dateStr=doc.select("div.main > div.main1 > div.contecter > div.con_left > div.pj_title").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.main > div.main1 > div.contecter > div.con_left > div.pj_contect"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.center > div.left > div.biaotii").text();
			dateStr=doc.select("div.center > div.left > div.laiyuan").text();
			content=doc.select("div.center > div.left > div > div.wenzhang").html();
			//content=HtmlCleaner.getContentHtml(url,Jsoup.parse(HttpUtils.htmlUnescape(content)));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.main > div.center > div.ccwe > div.zhengweng > div.biaoti").text();
			dateStr=doc.select("div.main > div.center > div.ccwe > div.zhengweng > div.laiyuan").text();
			content=doc.select("div.main > div.center > div.ccwe > div.zhengweng > div.wenzhang").html();
			//content=HtmlCleaner.getContentHtml(url,Jsoup.parse(HttpUtils.htmlUnescape(content)));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.main > div.main1 > div.main2 > div.left > h1").text();
			dateStr=doc.select("div.main > div.main1 > div.main2 > div.left > p.artInfo").text();
			content=doc.select("div.main > div.main1 > div.main2 > div.left > div.artCon").html();
		}


		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.main h1").text();
			dateStr=doc.select("div.main div.cont_laiyuan,div.pj_title").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.main div#fontzoom,div.pj_contect"));

		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#w980 > div.w650 > table > tbody > tr > td > div.title").text();
			dateStr=doc.select("div#w980 > div.w650 > table > tbody > tr > td > div").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#w980 > div.w650 > table > tbody > tr > td > div.text_w650"));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#Con620 div.title,div.BiaoTi").text();
			dateStr=doc.select("div#Con620,div.Con620 div").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#Con620 div.text,div.Content"));
		}


		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#jxnews_iframe div.contect div.newslb div.title").text();
			dateStr=doc.select("div#jxnews_iframe div.contect div.contleft div.newslb div.lyuan").text();
			content=doc.select("div#jxnews_iframe div.contect div.contleft div.ctxx p").html();
			//content=HtmlCleaner.getContentHtml(url,Jsoup.parse(HttpUtils.htmlUnescape(content)));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#fc_part2 td.BiaoTi").text();
			dateStr=doc.select("div#fc_part2 td.p14 table tr:eq(2)").text();
			content=doc.select("div#fc_part2 td.p14 table tr:eq(3)").html();
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.center div.biaotii").text();
			dateStr=doc.select("div.center div.laiyuan").text();
			content=doc.select("div.center div.wenzhang").html();
			//content=HtmlCleaner.getContentHtml(url,Jsoup.parse(HttpUtils.htmlUnescape(content)));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("table:eq(3) > tr >td > table > tr > td > table > tr > td > div > table:eq(1)").text();
			dateStr=doc.select("table:eq(3) > tr >td > table > tr > td > table > tr > td > div > table:eq(3)").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("table:eq(3) > tr >td > table > tr > td > table > tr > td > div > table:eq(4)"));
			//content=HtmlCleaner.getContentHtml(url,Jsoup.parse(HttpUtils.htmlUnescape(content)));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("td.title").text();
			dateStr=doc.select("font[color=blue]").text();;
			content=HtmlCleaner.getContentHtml(url,doc.select("div#_article"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("font#Zoom"));
			}
			//content=HtmlCleaner.getContentHtml(url,Jsoup.parse(HttpUtils.htmlUnescape(content)));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("body > table:eq(6) td.title").text();
			dateStr=doc.select("body > table:eq(6) table:eq(3)").text();
			if (dateStr==null || dateStr.length()<=0) {
				dateStr=doc.select("body > table:eq(6) table:eq(2)").text();
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("body > table:eq(6) table:eq(1)"));
			//content=HtmlCleaner.getContentHtml(url,Jsoup.parse(HttpUtils.htmlUnescape(content)));
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("td.tt_p1").text();
			dateStr=doc.select("span.p1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#_article"));
		}

		if(StringUtils.isBlank(title)){
			title=doc.select("h4").text();
			dateStr=doc.select("div.sc_title span").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.sc_contect"));
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("div.fb").text();
			dateStr=doc.select("div.sc_title").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.sc_contect"));
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("div.text_w665").text();
			dateStr=doc.select("font[color=#999999]").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#_article"));
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("div.p-ht").text();
			dateStr=doc.select("span.cBrown").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.text640"));
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			title=StringUtils.substringBeforeLast(title, "-");
		}

		date=DateUtils.matchDate(dateStr);
		if (date==null){
			date=DateUtils.matchDate(doc.select("#pubtime_baidu").text());
		}
		if (date==null){
			date=DateUtils.matchDate(doc.select("#pub_baidu").text());
		}
		if (date==null){
			date=DateUtils.matchDate(url);
		}

		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".artCon"));
		}
		NewsMeta news=new NewsMeta();
			news.setUrl(url);
			news.setType(1);
			news.setTitle(StringUtils.trimSpace(title));
			news.setDate(date);
			news.setContent(content);
			String author= JSoupUtils.matchAuthor(doc, "来源：");
			news.setAuthor(author);
			return news;
	}

	public static void main(String[] args) {
		JxnewsNewsAnalyse a=new JxnewsNewsAnalyse();
		String url="http://jiaju.jxnews.com.cn/system/2015/06/05/013931838.shtml";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
